package com.hospital.config;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.CommandLineRunner;
import org.springframework.context.annotation.Configuration;

import java.io.File;

/**
 * 文件上传配置
 */
@Slf4j
@Configuration
public class FileConfig implements CommandLineRunner {

    @Value("${file.upload.path}")
    private String uploadPath;

    @Override
    public void run(String... args) {
        // 创建上传根目录
        File uploadDir = new File(uploadPath);
        if (!uploadDir.exists()) {
            if (uploadDir.mkdirs()) {
                log.info("创建上传目录成功: {}", uploadPath);
            } else {
                log.error("创建上传目录失败: {}", uploadPath);
            }
        }

        // 创建子目录
        String[] subDirs = {"avatars", "images", "documents", "reports", "prescriptions", "others"};
        for (String dir : subDirs) {
            File subDir = new File(uploadPath, dir);
            if (!subDir.exists() && subDir.mkdirs()) {
                log.info("创建子目录成功: {}", subDir.getPath());
            }
        }
    }
} 